package com.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.beans.UserInfo;
import com.dao.UserDao;
import com.service.UserService;

@Controller
public class UserController {
	@Resource(name = "userServiceImpl")
	UserService userService;
	//用于国际化
	@Resource
	private MessageSource messageSource;
	@Resource
	UserDao userDao;
	@PostMapping("/login")
	public String login(String userName,String password,ModelMap m,String L) {
		if("admin".equals(userName) && "123".equals(password)) {
			return "main";
		}
		else {
			
			Locale locale=LocaleContextHolder.getLocale();  //得到当前的语言环境
			String msg =messageSource.getMessage("loginerror", null,locale);   //loginerror 是写在资源文件中的某条信息的key
			m.put("L", L);
			m.put("msg",msg);
			return "login"; 
		}
	}
	@GetMapping("/users")
	public String userManage(ModelMap m) {
		List<UserInfo> usersList=userService.getUserList();
		m.put("usersList",usersList);
		return "/user-pages/user-manager";
	}
	@RequestMapping("/varexpressions")
	public String varexpressions(ModelMap map) {
		map.put("xxxStr", "this is a cat");
		map.put("xxxBool", false);
		map.put("xxxArray", new Integer[] { 1, 2, 3, 4 });
		map.put("xxxList", Arrays.asList(1, 3, 2, 4, 0));

		Map<String, Object> xxxMap = new HashMap<String, Object>();
		xxxMap.put("第一", "陈聪");
		xxxMap.put("第二", "范鸽");
		map.put("xxxMap", xxxMap);

		map.put("xxxDate", new Date());
		map.put("xxxNum", 888.888D);
		return "thymeleaf-pages/demo2";
	}
	@RequestMapping("/thymeleaf")
	public String thymeleaf(ModelMap map) {
		map.put("thText", "th:text 设置文本内容 <b>加粗也没用因为不会转义</b>");
		map.put("thUText", "th:utext 设置文本内容 <b>加粗有用,会转义</b>");
		map.put("thValue", "thValue 设置当前元素的value值");
		map.put("thEach", Arrays.asList("香蕉", "芒果", "桃子", "杏", "苹果"));
		map.put("thIf", "msg is not null");
		map.put("thObject", new UserInfo(99, "张悦", "zhangyue123", "这是备注",0,new Date()));
		return "thymeleaf-pages/demo1";
	}
	@GetMapping("/user")
	public String user(ModelMap map) {
		map.put("user", new UserInfo());
		System.out.println("user");
		return "/user-pages/user-add";
	}
	@RequestMapping("/user/{id}")
	public String useredit(@PathVariable("id") Integer id,ModelMap map) {
		map.put( "user", userService.getUserById(id));
		return "/user-pages/user-edit";
	}
	@PostMapping("/user")
	public String addUser(ModelMap map, @ModelAttribute ("user")@Validated UserInfo user, BindingResult result) {
		if(result.hasErrors()) {	
			map.put("msg", "校验失败");
			List<FieldError> errorList=result.getFieldErrors();
			errorList.forEach(e->System.out.println(e.getField()+":"+e.getDefaultMessage()));				
		}
		else {
				System.out.println(user.getGender());
				userService.addUser(user);
				map.put("msg", "添加成功");

			
		}
		return "/user-pages/user-add";
	}
	@PostMapping("/edit")
	public String updateUser(ModelMap map, @ModelAttribute ("user")@Validated UserInfo user, BindingResult result) {
		System.out.println("12312332231231232332");
		if(result.hasErrors()) {	
			map.put("msg", "校验失败");
			List<FieldError> errorList=result.getFieldErrors();
			errorList.forEach(e->System.out.println(e.getField()+":"+e.getDefaultMessage()));				
		}
		
			else
			{
				userService.updateUser(user);
				map.put("msg", "更新成功");
			}
			
		return "/user-pages/user-edit";
	}
	@RequestMapping("/delete/{id}")
	public String deleteUser(@PathVariable("id") Integer id,ModelMap map) {
		userService.deleteUser(id);
		return "forward:/users";
	}
}
